package com.tj.test.sys.controller;

import com.tj.test.sys.po.Person;
import com.tj.test.sys.service.impl.PersonService;
import com.tj.test.sys.tool.ResponseMessage;
import com.tj.test.sys.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
/*对外提供数据服务接口*/
@Api(tags="person")
@Controller
@CrossOrigin
@RequestMapping("/person")
public class PersonController {
    @Autowired
    private PersonService service;

    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询所有人员信息")
    //查询所有个人信息
    public ResponseMessage selectAllPerson(){
        return Result.success(service.selectAllPerson());}

   /* @RequestMapping(value = "/selectById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("根据id查询人员信息")
    //根据id查询个人信息
    public Message<Person> selectPersonById(long perId){return service.selectPersonById(perId);}*/

    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("插入人员信息")
    //插入个人信息
    public ResponseMessage insertPerson(Person person) {
        int temp=0;
        //传入数据的判断
        //(1)实体对象不能为空
        if (person != null) {
            //（2）人员身份证号，账户，密码不能为空
            if (person.getPwd() != null & person.getAccount() != null & person.getPersonidcard() != null) {
                //(3)添加人员信息
                temp=service.insertPerson(person);
                if (temp>0) {return Result.success("插入成功",temp);}
            }
        }
        return Result.error("插入数据失败",temp);
    }
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("删除人员信息")
    //删除个人信息
    public ResponseMessage deletePerson(Person person){
        int temp=0;
        try {
            if (person!=null){
                temp=service.deletePerson(person);
                if (temp>0) {
                    return Result.success("删除成功",temp);
                }else {
                return Result.error("删除失败，未找到该条信息");}
            }
            }
        catch (Exception e){
             temp=-1;
             return Result.error("删除失败，数据库异常",temp);
        }
        return Result.error("删除失败，数据为空",temp);}

    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("更新人员信息")
    //修改个人信息
    public ResponseMessage updatePerson(Person person){
        int temp=0;
        try {
            if (person!=null){
                temp=service.updatePerson(person);
                if (temp>0) {
                    return Result.success("更新成功",temp);
                }else {
                    return Result.error("更新失败，未找到该条信息");}
            }
        }
        catch (Exception e){
            temp=-1;
            return Result.error("更新失败，数据库异常",temp);
        }
        return Result.error("更新失败，数据为空",temp);}
}
